package com.dayuanit.dy17.mix.aop.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Order(Integer.MAX_VALUE)
public class TransactionalAspect {

    @Pointcut("@annotation(com.dayuanit.dy17.mix.aop.annotation.Transactional)")
    public void pointCut() {

    }

    @Around("pointCut()")
    public void doAround(ProceedingJoinPoint joinPoint) {
        //实际上是要从数据源中获取Connection对象，然后将setAutoComimt置为false,
        //那么就开启了事务，但是，Connection要在Mapper中使用，而Service又不接收Connection
        //参数，所以，这里就会将得到的Connetion对象放到本地线程容器中，也就是ThreadLocal对象。
        System.out.println("开启事务...");

        try {
            //执行目标方法
            Object result = joinPoint.proceed();
            System.out.println("提交事务");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("回滚事务");
        }

    }
}
